package com.ssplayer.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssplayer.activity.R;
import com.ssplayer.bean.Music;
import com.ssplayer.utils.MediaUtils;

public class MusicAdapter extends BaseAdapter {

	private Context context;
	private List<Music> musics;
	private Music music;

	public MusicAdapter() {
	}

	public MusicAdapter(Context context, List<Music> musics) {
		this.musics = musics;
		this.context = context;
	}

	@Override
	public int getCount() {
		return musics.size();
	}

	@Override
	public Object getItem(int position) {
		return musics.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint({ "NewApi", "InflateParams" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MusicHolder musicHolder = null;
		if (convertView == null) {
			musicHolder = new MusicHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.music_item, null);
			musicHolder.music_album = (ImageView) convertView
					.findViewById(R.id.music_album);
			musicHolder.music_title = (TextView) convertView
					.findViewById(R.id.music_title);
			musicHolder.music_artist = (TextView) convertView
					.findViewById(R.id.music_artist);
			musicHolder.music_duration = (TextView) convertView
					.findViewById(R.id.music_duration);
			convertView.setTag(musicHolder);
		} else {
			musicHolder = (MusicHolder) convertView.getTag();
		}
		music = musics.get(position);
		musicHolder.music_album.setBackground(context.getResources()
				.getDrawable(R.drawable.album));
		musicHolder.music_title.setText(music.getTitle());
		musicHolder.music_artist.setText(music.getArtist());
		musicHolder.music_duration.setText(MediaUtils.formatDuration(music
				.getDuration()));
		return convertView;
	}

	private class MusicHolder {
		ImageView music_album;
		TextView music_title;
		TextView music_artist;
		TextView music_duration;
	}

}
